package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.entity.ActiviteEntity;
import com.kuanter.kuanterauto.main.HomeActivityGroup;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.kuanter.kuanterauto.utils.StringUtils;

/** 
 * @author utopia
 * @version 2013-9-22 上午9:03:08
 * 
 */
public class NewActivation extends BaseActivity_NoBar {
	TextView top_middle;
	LinearLayout home_chose_City;
	
	EditText active_phone;
	EditText active_password;
	TextView button_center;
	
	boolean hasLogin = false;
	
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				if(!SharePrefenceUtil.checkLogin(NewActivation.this)) {
					finishProgress();
					String code = "";
					String hasJYM = "0";
					String userPassword = "";
					try {
						JSONObject jo = new JSONObject(json);
						if(StringUtils.isSuccess(json)) {
							JSONObject messgae = jo.optJSONObject("message");
							code = messgae.optString("randomCode");
							hasJYM = messgae.optString("validateCode");
							userPassword = messgae.optString("userPassword");
							System.err.println(hasJYM + "安全码!!!!!!!!!!!!");
							
							JSONObject jo_userVehicle = messgae.optJSONObject("userVehicle");
							ActiviteEntity ae = null;
							if(jo_userVehicle != null) {
								ae = new ActiviteEntity();
								ae.setLicensePlateNumber(jo_userVehicle.optString("licensePlateNumber"));
								ae.setVehicleBrand(jo_userVehicle.optString("vehicleBrand"));
								ae.setVehicleModels(jo_userVehicle.optString("vehicleModels"));
							}
							
							JSONObject jo_userInfo = messgae.optJSONObject("userInfo");
							if(jo_userInfo != null) {
								if(ae == null) {
									ae = new ActiviteEntity();
								}
								ae.setGender(jo_userInfo.optInt("gender"));
								ae.setUserRealName(jo_userInfo.optString("userRealName"));
							}
							
								Intent intent_next = new Intent(NewActivation.this, NewActivationNext.class);
								intent_next.putExtra("hasJYM", hasJYM);
								intent_next.putExtra("code", code);
								intent_next.putExtra("userPassword", userPassword);
								intent_next.putExtra("mobileNumber", active_phone.getText().toString());
								intent_next.putExtra("cardNumber", active_password.getText().toString());
								intent_next.putExtra("ae", ae);
								startActivity(intent_next);
								
						} else {
							String result = jo.optString("errorMsg");
							showToast(result);
						}
					} catch (JSONException e) {
						showToast("数据提交失败");
						e.printStackTrace();
					}
				} else {
					try {
						if(StringUtils.isSuccess(json)) {
							JSONObject jo = new JSONObject(json);
							String hasJYM = jo.optString("validateCode");
							if(!hasJYM.equals("0") && !hasJYM.equals("")) {
								finishProgress();
								Intent intent_next = new Intent(NewActivation.this, NewActivationNext.class);
								intent_next.putExtra("hasJYM", hasJYM);
								intent_next.putExtra("cardNumber", active_password.getText().toString());
								startActivity(intent_next);
							} else {
								showToast("激活成功!");
								if(ProApplication.getInstance().getCardInfoList() != null) {
									ProApplication.getInstance().getCardInfoList().clear();  //清空卡片信息刷新数据
								}
								finish();
								/*DataLoader loader = new DataLoader(NewActivation.this);
								List<NameValuePair> params = new ArrayList<NameValuePair>();
								
								SharedPreferences sp = getSharedPreferences(SharePrefenceUtil.USERINFO, MODE_PRIVATE);
								String phoneNumber = sp.getString("mobileNumber", "");
								NameValuePair nvp_phone = new BasicNameValuePair("mobileNumber",phoneNumber);
								params.add(nvp_phone);
								
								NameValuePair nvp_cardNumber = new BasicNameValuePair("cardNumber",active_password.getText().toString());
								params.add(nvp_cardNumber);
								loader.DisplayData(HttpConfigAPI.checkActivationCard, params, handler, "", false);*/
								
							}
						} else {
							finishProgress();
							JSONObject jo = new JSONObject(json);
							String result = jo.optString("errorMsg");
							showToast(result);
						}
					} catch (JSONException e) {
						finishProgress();
						e.printStackTrace();
					}
				}
			} else if(msg.what == HttpTask.RESULT_ERROR) {
				finishProgress();
				showToast("网络错误,获取数据失败");
			}
			super.handleMessage(msg);
		}
		
	};
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			finishProgress();
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				try {
					if(StringUtil.isSuccess(json)) {
						showToast("激活成功!");
						if(ProApplication.getInstance().getCardInfoList() != null) {
							ProApplication.getInstance().getCardInfoList().clear();  //清空卡片信息刷新数据
						}
						finish();
					} else {
						JSONObject jo = new JSONObject(json);
						String result = jo.optString("errorMsg");
						showToast(result);
					}
				} catch (JSONException e) {
					showToast("网络错误,激活失败");
					e.printStackTrace();
				}
			} else if(msg.what == HttpTask.RESULT_ERROR) {
				showToast("网络错误,获取数据失败");
			}
			super.handleMessage(msg);
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activate_card_new);
		initTop();
		initUI();
	}
	
	@Override
	protected void onResume() {
		hasLogin = SharePrefenceUtil.checkLogin(NewActivation.this);
		if(hasLogin) {
			active_phone.setVisibility(View.GONE);
			button_center.setText("确认卡号");
		} else {
			active_phone.setVisibility(View.VISIBLE);
			button_center.setText("收取验证码");
		}
		super.onResume();
	}



	private void initUI() {
		active_phone = (EditText) findViewById(R.id.active_phone);
		active_password = (EditText) findViewById(R.id.active_password);
		button_center = (TextView) findViewById(R.id.button_center);
		button_center.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				if(!hasLogin) {
					if(!isPhoneNum(active_phone.getText().toString().trim())) {
						showToast("请输入正确的手机号码");
						return;
					} else {
						NameValuePair nvp_phone = new BasicNameValuePair("mobileNumber",active_phone.getText().toString().trim());
						params.add(nvp_phone);
					}
				} else {
					SharedPreferences sp = getSharedPreferences(SharePrefenceUtil.USERINFO, MODE_PRIVATE);
					String phoneNumber = sp.getString("mobileNumber", "");
					NameValuePair nvp_phone = new BasicNameValuePair("mobileNumber",phoneNumber);
					params.add(nvp_phone);
				}
				 if(TextUtils.isEmpty(active_password.getText().toString())) {
						showToast("请输入卡号");
				 } else {
						//TODO  提交
					 	showProgress("正在提交数据...");
						DataLoader loader = new DataLoader(NewActivation.this);
						NameValuePair nvp_cardNum = new BasicNameValuePair("cardNumber",active_password.getText().toString());
						params.add(nvp_cardNum);
						String url = "";
						if(!SharePrefenceUtil.checkLogin(NewActivation.this)) {
							url = HttpConfigAPI.NOTLGOINActivation;
							NameValuePair nvp_flag = new BasicNameValuePair("flag", "true");
							params.add(nvp_flag);
						} else {
							url = HttpConfigAPI.LGOINActivation;
						}
						loader.DisplayData(url, params, mHandler, "", false);
				 }
			}
		});
	}

	private void initTop() {
		top_middle = (TextView) findViewById(R.id.top_middle);
		top_middle.setText("激活");
		
		home_chose_City = (LinearLayout) findViewById(R.id.home_chose_City);
		home_chose_City.setVisibility(View.GONE);
	}

	public void exit(View view) {
		finish();
	}

	@Override
	protected void onDestroy() {
		finishProgress();
		super.onDestroy();
	}
	
	
}
